<template>
    <div class="topicEdit" v-loading="loading">
        <h1>编辑题目</h1>
        <div class="top">
            <div class="left">
                <div class="form">
                    <el-form :model="topic" label-width="120px" style="width: calc(92%);">
                        <el-form-item label="标题">
                            <el-input v-model="topic.title" placeholder="请输入标题"></el-input>
                        </el-form-item>
                        <el-form-item label="难度">
                            <el-rate
                                style="padding-top: 10px;"
                                v-model="topic.level"
                                >
                            </el-rate>
                        </el-form-item>
                    </el-form>
                </div>
                <div class="editor">
                    <vue-editor v-model="topic.content" style="height: 500px;"></vue-editor>
                </div>
            </div>
            <div class="right">
                <el-form :model="topic" label-width="80px" style="width: calc(92%);">
                    <el-form-item :label="'实例：' + (index + 1)" v-for="(item, index) in topicResult" :key="index"
                    class="item"
                    style="">
                        <div class="params" v-for="(it, i) in item.params" :key="i">
                            <el-select v-model="it.dataType"
                            placeholder="请选择数据类型">
                                <el-option
                                v-for="(item,i) in dataType"
                                :key="item"
                                :label="item"
                                :value="item">
                                </el-option>
                            </el-select>
                            <el-input v-model="it.data" placeholder="参数数据"></el-input>
                        </div>
                        <div style="padding: 10px;display: flex;justify-content: center;">
                            <el-button @click="addResultParams(item.params)"><i class="el-icon-plus"></i></el-button>
                            <el-button @click="delResultParams(item.params)" v-if="item.params.length > 0"><i class="el-icon-minus"></i></el-button>
                            
                            <el-button type="danger" @click="delResult(index)"><i class="el-icon-close"></i></el-button>
                            <el-button @click="saveResult([item])" v-if="item.id && topic.id" type="primary"><i class="el-icon-check"></i></el-button>
                        </div>
                        <div class="result">
                            <el-select v-model="item.result.dataType"
                            placeholder="请选择数据类型">
                                <el-option
                                v-for="(item,i) in dataType"
                                :key="item"
                                :label="item"
                                :value="item">
                                </el-option>
                            </el-select>
                            <el-input v-model="item.result.data" placeholder="结果数据"></el-input>
                        </div>
                    </el-form-item>
                    <el-form-item v-if="topicResult.length == 0">
                        <el-empty description="请添加实例"></el-empty>
                    </el-form-item>
                    <el-form-item>
                        <div style="display: flex;justify-content: center;">
                            <el-button type="primary" @click="addResult">新增实例 <i class="el-icon-plus"></i></el-button>
                        </div>
                    </el-form-item>
                </el-form>
            </div>
        </div>
        <div class="bottom">
            <el-button type="primary" @click="drawer = true">预览</el-button>
            <el-button type="primary" @click="edit">保存</el-button>
        </div>
		<topicPreview :topic="topic" :drawer="drawer" @handleClose="drawer = false"/>
    </div>
</template>

<script>
    import { VueEditor } from "vue2-editor"
    import { editTopic, getTopicDetail } from '@/api/topic'
    import { editTopicResult, delTopResult } from '@/api/topicResult'
    import { stringToResult, resultToString } from '@/assets/js/topicResult'
    import { debounce } from '@/utils/tool'
    import topicPreview from "@/components/topic/topicPreview.vue"
    export default {
        components : {
            VueEditor,
            topicPreview
        },
        data() {
            return {
                drawer : false,
                id : undefined,
                topic : {
                    title : '',
                    level : 0,
                    content : '',
                    fraction : 0
                },
                topicResult : [],
                fractions : [1, 2, 4, 8, 16],
                dataType : [
                    'int',
                    'double',
                    'String',
                    'int[]',
                    'double[]',
                    'String[]',
                ],
                loading : false
            }
        },
        created(){
            this.edit = debounce(this.edit)
            if (this.$route.query.id) {
                this.id = this.$route.query.id
                this.getDetail(this.id)
            }
        },
        methods : {
            getDetail(id){
                this.loading = true
                getTopicDetail(id).then(res => {
                    this.loading = false
                    if(res.code == 200){
                        this.topic = res.data.topic
                        this.topicResult = stringToResult(res.data.result)
                    }
                })
            },
            edit(){
                if (!this.topic.title) {
                    this.$message.error('请输入标题')
                    return
                }
                if (!this.topic.level) {
                    this.$message.error('请选择难度')
                    return
                }
                if (!this.topic.content) {
                    this.$message.error('请输入题目内容')
                    return
                }


                let list = resultToString(this.topicResult)

                if(typeof list == 'string'){
                    this.$message.error(list)
                    return
                }

                this.loading = true

                editTopic(this.topic).then(res => {
                    if (res.code == 200) {
                        this.editResult(list, res.data, () => {
                            this.loading = false
                            this.$router.push('/admin/topicList')
                            this.$message.success('保存成功')
                        })
                    }
                })

            },
            editResult(list, id, fn){
                if (list.length == 0) {
                    fn()
                    return
                }
                let data = list.pop()
                data.topicId = id

                editTopicResult(data).then(res => {
                    if (res.code == 200) {
                        this.editResult(list, id, fn)
                    }
                })
            },
            saveResult(data){
                let list = resultToString(data)
                if(typeof list == 'string'){
                    this.$message.error(list)
                    return
                }
                this.loading = true
                this.editResult(list, this.topic.id, () => {
                    this.loading = false
                    this.$message.success('保存成功')
                    this.getDetail(this.topic.id)
                })
            },
            addResult(){
                let data = {
                    params : [
                        {
                            dataType : '',
                            data : ''
                        }
                    ],
                    result : {
                        dataType : '',
                        data : ''
                    },
                }
                this.topicResult.push(data)
            },
            delResult(index){
                this.$confirm('确认删除？')
                .then(() => {
                    let data = this.topicResult.splice(index, 1)[0]
                    if (data.id) {
                        this.loading = true
                        delTopResult(data.id).then(res => {
                            this.loading = false
                            if(res.code == 200){
                                this.$message.success('删除成功')
                            }
                        })
                    }
                })
                .catch(() => {});
            },
            addResultParams(data){
                data.push({
                    dataType : '',
                    data : ''
                })
            },
            delResultParams(data){
                data.pop()
            }
        }
    }
</script>

<style lang="scss" scoped>
@media screen and (max-width: 1000px) {
    .topicEdit{
        padding: 20px 0;
        .top {
            flex-direction: column;
        }
    }
}
@media screen and (min-width: 1000px) {
    .topicEdit{
        padding: 20px;
        padding-bottom: 40px;
    }
}
.topicEdit{
    background-color: #fff;
    min-height: calc(100vh - 220px);
    position: relative;

    .top{
        display: flex;
        .left{
            flex: 1;
            padding-bottom: 30px;
            .form{
                display: flex;
                align-items: center;
            }
            .editor{
                width: calc(100%);
                height: 600px;
            }
        }
        .right{
            flex: 1;
            display: flex;
            flex-direction: column;
            align-items: center;
            .item{
                box-shadow: 0 2px 4px rgba(0, 0, 0, .12), 0 0 6px rgba(0, 0, 0, .04);
                padding: 20px 10px;
            }
            .result,
            .params{
                display: flex;
                div{
                    flex: 1;
                }
            }
        }
        padding-bottom: 50px;
    }
    .bottom{
        position: absolute;
        left: 20px;
        bottom: 20px;
    }
}
</style>